package com.spooky.esb;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlUtil {
	/**
	 * @param .Should be followed by createRoot()
	 * @return Creates a new Document that contains XML
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 */
	public static Document createDocument(String version, String encoding) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document parse = builder.newDocument();
		parse.setXmlVersion(version);
		return parse;
	}

	public static Document readDocument(String filePath) throws ParserConfigurationException, SAXException, IOException {
		File file = new File(filePath);
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
		Document document = documentBuilder.parse(file);
		document.getDocumentElement().normalize();
		return document;
	}

	public static Element getRootElement(Document document) {
		return document.getDocumentElement();
	}

	public static Element createRoot(Document document, String root) {
		Element rootElement = document.createElement(root);
		document.appendChild(rootElement);
		return rootElement;
	}

	public static Element createRootNamespace(Document document, String namespaceAlias, String namespace, String root) {
		Element rootElement = document.createElementNS(namespace, root);
		rootElement.setPrefix(namespaceAlias);
		document.appendChild(rootElement);
		return rootElement;
	}

	public static Element addElement(Element root, String name) {
		Element newElement = root.getOwnerDocument().createElement(name);
		root.appendChild(newElement);
		return newElement;
	}

	public static Element addElementNamespace(Element root, String namespaceAlias, String namespace, String name) {
		Element newElement = root.getOwnerDocument().createElementNS(namespace, name);
		newElement.setPrefix(namespaceAlias);
		root.appendChild(newElement);
		return newElement;
	}

	public static Element setAttribute(Element element, String key, String value) {
		element.setAttribute(key, value);
		return element;
	}

	public static void createFile(Document document, String filePath) throws TransformerException {
		TransformerFactory tranFactory = TransformerFactory.newInstance();
		Transformer aTransformer = tranFactory.newTransformer();
		Source src = new DOMSource(document);
		Result dest = new StreamResult(new File(filePath));
		aTransformer.transform(src, dest);
	}

	public static List<Element> getChilds(Element root) {
		NodeList childNodes = root.getChildNodes();
		List<Element> elementList = new LinkedList<Element>();
		for (int i = 0; i < childNodes.getLength(); ++i) {
			elementList.add((Element) childNodes.item(i));
		}
		return elementList;
	}

	public static List<Element> getChildsWithName(Element root, String name) {
		NodeList childNodes = root.getElementsByTagName(name);
		List<Element> elementList = new LinkedList<Element>();
		for (int i = 0; i < childNodes.getLength(); ++i) {
			elementList.add((Element) childNodes.item(i));
		}
		return elementList;
	}

	public static Element getElement(Element root, String name) throws Exception {
		NodeList elements = root.getElementsByTagName(name);
		if (elements.getLength() > 0) {
			return (Element) elements.item(0);
		}
		throw new Exception("Child element '" + name + "' to parent " + root.getNodeName() + " not found.");
	}

	public static String getValue(Element element) throws Exception {
		String value = element.getTextContent();
		if (value != null) {
			return value;
		}
		throw new Exception("'" + element.getNodeName() + "' empty value or no value.");
	}
}